package com.hlt.shuiliting.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlt.shuiliting.dao.GongChengXMDAO;
import com.hlt.shuiliting.dao.XiangMuSPDAO;
import com.hlt.shuiliting.valuebean.XiangMuSP;

public class XiangMuSPServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public XiangMuSPServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setCharacterEncoding("UTF-8");//�������Ĳ���

		String servletPath = request.getServletPath();
		if (servletPath.equalsIgnoreCase("/listXiangMuSP")) {
			listXiangMuSP(request, response);
		} else if (servletPath.equalsIgnoreCase("/viewXiangMuSP")) {
			viewXiangMuSP(request, response);
		} else if (servletPath.equalsIgnoreCase("/insertXiangMuSP")) {
			insertXiangMuSP(request, response);
		} else if (servletPath.equalsIgnoreCase("/updateXiangMuSP")) {
			updateXiangMuSP(request, response);
		} else if (servletPath.equalsIgnoreCase("/deleteXiangMuSP")) {
			deleteXiangMuSP(request, response);
		}
	}
	
	public void listXiangMuSP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangMuSPDAO xiangMuSPDAO = new XiangMuSPDAO();
		GongChengXMDAO gongChengXMDAO = new GongChengXMDAO();
		
		String pageSize = request.getParameter("pageSize");
		String pageNum = request.getParameter("pageNum");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		List list = xiangMuSPDAO.list(Integer.parseInt(GongChengXMID), (Integer.parseInt(pageSize)), (Integer.parseInt(pageNum)));
		Map map = (Map)gongChengXMDAO.view(Integer.parseInt(GongChengXMID));
		String pageCount = Integer.toString(xiangMuSPDAO.getPageCount());
		
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("gongChengXM", map);
		request.setAttribute("xiangMuSPLIST", list);
		
		request.getRequestDispatcher("/XiangMuSP_List.jsp").forward(request, response);
	}
	
	public void viewXiangMuSP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangMuSPDAO xiangMuSPDAO = new XiangMuSPDAO();
		String ID = request.getParameter("ID");
		String viewOrUpdate = request.getParameter("viewOrUpdate");
		Map map = xiangMuSPDAO.view(Integer.parseInt(ID));
		request.setAttribute("xiangMuSP", map);
		
		if (viewOrUpdate.equalsIgnoreCase("view")) {
			request.getRequestDispatcher("XiangMuSP_View.jsp").forward(request, response);
		} else if (viewOrUpdate.equalsIgnoreCase("update")) {
			request.getRequestDispatcher("XiangMuSP_Update.jsp").forward(request, response);
		}
	}
	
	public void insertXiangMuSP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangMuSPDAO xiangMuSPDAO = new XiangMuSPDAO();
		XiangMuSP xiangMuSP = new XiangMuSP();
		
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String LeiXing = request.getParameter("LeiXing");
		String PiFuWH = request.getParameter("PiFuWH");
		String PiWenBT = request.getParameter("PiWenBT");
		String PiFuSJ = request.getParameter("PiFuSJ");
		String PiFuBM = request.getParameter("PiFuBM");
		String TouZiE = request.getParameter("TouZiE");
		String WanChengDWMC = request.getParameter("WanChengDWMC");
		String FuHeWH = request.getParameter("FuHeWH");
		String FuHeJG = request.getParameter("FuHeJG");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		xiangMuSP.setZhuangTai(ZhuangTai);
		xiangMuSP.setJiLuSJ(JiLuSJ);
		xiangMuSP.setLuRuR(LuRuR);
		xiangMuSP.setLeiXing(LeiXing);
		xiangMuSP.setPiFuWH(PiFuWH);
		xiangMuSP.setPiWenBT(PiWenBT);
		xiangMuSP.setPiFuSJ(PiFuSJ);
		xiangMuSP.setPiFuBM(PiFuBM);
		xiangMuSP.setTouZiE(TouZiE);
		xiangMuSP.setWanChengDWMC(WanChengDWMC);
		xiangMuSP.setFuHeWH(FuHeWH);
		xiangMuSP.setFuHeJG(FuHeJG);
		xiangMuSP.setGongChengXMID(GongChengXMID);
		xiangMuSPDAO.insert(xiangMuSP);
		
		response.sendRedirect("listXiangMuSP?GongChengXMID=" + GongChengXMID);
	}
	
	public void updateXiangMuSP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangMuSPDAO xiangMuSPDAO = new XiangMuSPDAO();
		XiangMuSP xiangMuSP = new XiangMuSP();
		
		String ID = request.getParameter("ID");
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String LeiXing = request.getParameter("LeiXing");
		String PiFuWH = request.getParameter("PiFuWH");
		String PiWenBT = request.getParameter("PiWenBT");
		String PiFuSJ = request.getParameter("PiFuSJ");
		String PiFuBM = request.getParameter("PiFuBM");
		String TouZiE = request.getParameter("TouZiE");
		String WanChengDWMC = request.getParameter("WanChengDWMC");
		String FuHeWH = request.getParameter("FuHeWH");
		String FuHeJG = request.getParameter("FuHeJG");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		xiangMuSP.setID(ID);
		xiangMuSP.setZhuangTai(ZhuangTai);
		xiangMuSP.setJiLuSJ(JiLuSJ);
		xiangMuSP.setLuRuR(LuRuR);
		xiangMuSP.setLeiXing(LeiXing);
		xiangMuSP.setPiFuWH(PiFuWH);
		xiangMuSP.setPiWenBT(PiWenBT);
		xiangMuSP.setPiFuSJ(PiFuSJ);
		xiangMuSP.setPiFuBM(PiFuBM);
		xiangMuSP.setTouZiE(TouZiE);
		xiangMuSP.setWanChengDWMC(WanChengDWMC);
		xiangMuSP.setFuHeWH(FuHeWH);
		xiangMuSP.setFuHeJG(FuHeJG);
		xiangMuSP.setGongChengXMID(GongChengXMID);
		xiangMuSPDAO.update(xiangMuSP);
		
		response.sendRedirect("listXiangMuSP?GongChengXMID=" + GongChengXMID);
	}
	
	public void deleteXiangMuSP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangMuSPDAO xiangMuSPDAO = new XiangMuSPDAO();
		String ID = request.getParameter("ID");
		String GongChengXMID = request.getParameter("GongChengXMID");
		xiangMuSPDAO.delete(Integer.parseInt(ID));
		response.sendRedirect("listXiangMuSP?GongChengXMID=" + GongChengXMID);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
